// pages/teacher/reserveDetail/index.js
const app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    reserveStatus: [],
    selectedStatus: null,
    reserveDetail: null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let that = this;

    this.setData({
      reserveStatus: [
        { id: 1, name: "到达" },
        { id: 2, name: "迟到" },
        { id: 3, name: "爽约" }
      ],

      reserveDetail: JSON.parse(options.obj)
    });

    wx.request({
      url: app.globalData.serverUrl + "/reserve/queryReserveDetailById",
      header: {
        token: app.globalData.token
      },
      method: "GET",
      data: { reserveDetailId: this.data.reserveDetail.id },
      success(resp) {
        console.log(resp);

        that.data.reserveStatus.forEach(element => {
          if (element.id == resp.data.status) {
            that.setData({
              selectedStatus: element
            });
          }
        });

        that.setData({
          ["reserveDetail.status"]: resp.data.status,
          ["reserveDetail.remarks"]: resp.data.remarks
        });

        console.log(that.data.reserveDetail);
      },
      fail(resp) {
        console.error(resp);
      }
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {},

  bindPickerChange(e) {
    this.setData({
      selectedStatus: this.data.reserveStatus[e.detail.value]
    });
  },

  makeCall(event) {
    console.log(event.currentTarget);
    if (event.currentTarget.dataset.phonenu != null) {
      wx.makePhoneCall({
        phoneNumber: event.currentTarget.dataset.phonenu,
        success: function() {
          console.log("拨打电话成功！");
        },
        fail: function() {
          console.log("拨打电话失败！");
        }
      });
    }
  },

  formSubmit1(e) {
    console.log(e);

    let obj = {};
    obj.id = this.data.reserveDetail.id;
    obj.status = this.data.selectedStatus.id;
    obj.remarks = e.detail.value.intro;

    wx.request({
      url: app.globalData.serverUrl + "/reserve/updateUserClassStatus",
      header: {
        token: app.globalData.token
      },
      method: "POST",
      data: obj,
      success(resp) {
        console.log(resp);
        if (resp.data.success) {
          wx.showToast({
            title: "操作成功",
            icon: "none"
          });

        } else {
          wx.showToast({
            title: "操作失败",
            icon: "none"
          });
        }

      },
      fail(resp) {
        console.error(resp);
        wx.showToast({
          title: "异常: " + JSON.stringify(resp),
          icon: "none"
        });
      }
    });
  }
});
